//
//  CategoryTimeViewController.m
//  openEye
//
//  Created by 徐悟源 on 16/3/31.
//  Copyright © 2016年 Wuyuan. All rights reserved.
//

#import "CategoryTimeViewController.h"
#import "PlayerViewController.h"
#import "Video.h"
#import "VideoCell.h"
#import "Categories.h"
#import "MJRefresh.h"
#import "Daily.h"

@interface CategoryTimeViewController ()

@property (nonatomic , strong) NSMutableArray *videoList;

@end

@implementation CategoryTimeViewController

- (NSMutableArray *)videoList
{
    if(!_videoList)
    {
        _videoList = [NSMutableArray array];
    }
    return _videoList;
}

- (void)loadVideoData
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"num"] = @"10";
    params[@"categoryName"] = self.category.name;
    params[@"strategy"] = @"date";
    params[@"start"] = [NSString stringWithFormat:@"%lu",self.videoList.count];
    
    __weak typeof(self) weakSelf = self;
    
    [[AFHTTPSessionManager manager] GET:@"http://baobab.wandoujia.com/api/v1/videos?" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        weakSelf.videoList = [Video mj_objectArrayWithKeyValuesArray:responseObject[@"videoList"]];
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}

- (void)addRefresh
{
     self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerWithRefreshingMore)];
}

- (void)footerWithRefreshingMore
{
    __weak typeof(self) weakSelf = self;
    [[AFHTTPSessionManager manager] GET:[Video toPathFromCategorie] parameters:[Video toParameterFromCategorie:self.category WithController:self WithLength:self.videoList.count] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        
        
        NSArray *array = responseObject[@"videoList"];
        if(array.count == 0)
        {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        else
        {
            [weakSelf.videoList addObjectsFromArray:[Video mj_objectArrayWithKeyValuesArray:array]];
            
            [weakSelf.tableView reloadData];
            
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadVideoData];
    
    [self addRefresh];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.videoList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    VideoCell *cell = [VideoCell cellWithTableView:tableView];
    
    cell.video = self.videoList[indexPath.row];
    
    // Configure the cell...
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CellH;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerViewController *pv = [[PlayerViewController alloc] init];
    
    Video *video = self.videoList[indexPath.row];
    pv.video = video;
    
    [self presentViewController:pv animated:YES completion:nil];
}

@end
